#!/bin/bash

INPUT=$1

usage(){
[ "$1" = "1" ] && echo "File $INPUT not found"
echo "Usage : $0 imovie_project_dir"
echo "Usage : $0 \"/Volumes/iMovie/iMovie Projects.localized\""
exit 1
}

shellout(){
echo "$1"
exit 1
}

[ "$INPUT" = "" ] && usage 0
[ -d "$INPUT" ] || usage 1

MYPWD=`pwd`
echo "Running from $MYPWD"
echo "cd $INPUT"
cd "$INPUT" || shellout "error while running command : cd $INPUT"
ls | while read dir
do

if [ -d "$dir" ]
then
    echo "Processing directory $dir"
    [ -d "${dir}/Movies/" ] && echo "cd \"${dir}/Movies/\"" && cd "${dir}/Movies/" && find . -type f ! -type l -name "*.m*v" | while read file
    do
            OLD=`echo "$file " | sed -e "s/^.\///g"`
            OLD=`echo "$OLD " | sed -e "s/ *$//g"`
            NEW=`echo $dir | sed -e "s/.rcproject//g"` 
            NEW="$NEW-$OLD"
            echo command : ln -sf \"$OLD\" \"$NEW\"
            ln -sf "$OLD" "$NEW"
    done

fi
echo "cd $MYPWD"
cd "$MYPWD" || shellout "error while running command : cd $MYPWD"
echo "cd $INPUT"
cd "$INPUT" || shellout "error while running command 2 : cd $INPUT"
done 

echo
echo "_____________________________________________"
echo "List of all symbolic link in directory : $INPUT"
echo "cd $MYPWD"
cd "$MYPWD" || shellout "error while running command : cd $MYPWD"
find "$INPUT" -type l | while read line
do
    echo "cp -nv \"$line\" \"/Volumes/My Passport/Videos/\""
done
echo
